package com.qzdsoft.erpcloud.service.sys.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.sys.SysRole;
import com.qzdsoft.erpcloud.domain.sys.SysRoleMenu;
import com.qzdsoft.erpcloud.mapper.sys.SysRoleMapper;
import com.qzdsoft.erpcloud.mapper.sys.SysRoleMenuMapper;
import com.qzdsoft.erpcloud.service.sys.SysRoleService;
import com.qzdsoft.erpcloud.vo.sys.SysRoleInfo;
import com.qzdsoft.erpcloud.vo.sys.SysRoleQueryParam;
import com.qzdsoft.utils.enums.CommonStatus2Enum;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.sys.SysRoleStatus;

/**
 * Created by Yang
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {
	private static final Logger logger = LoggerFactory.getLogger(SysRoleServiceImpl.class);
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuMapper  sysRoleMenuMapper;
    
    @Override
    public SysRole selectById(Integer id) {
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysRole> getAllRoleList() {
        return sysRoleMapper.selectAll();
    }


    @Override
    public Response saveOrUpd(SysRole sysRole) {

        if(null != sysRole.getId()){        //  更新
//        	 SysRole role = selectById(sysRole.getId());
//        	if(role != null) {
        	int count = sysRoleMapper.updateByPrimaryKeySelective(sysRole);
//        	}
        } else {
        	sysRole.setCtime(new Date());
        	 sysRole.setAllFunction(0);
             sysRole.setAllData(0);
            sysRoleMapper.insert(sysRole);
        }
        return Response.success(null);
    }

    @Override
    public Response del(Integer id) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
        sysRole.setStatus(Integer.parseInt(SysRoleStatus.DEL_CODE));
        sysRoleMapper.updateByPrimaryKey(sysRole);
        return Response.success(null);
    }

	@Override
	public LayTableResponse<SysRoleInfo> selectAllData(SysRoleQueryParam info) {
		PageHelper.startPage(info.getPage(), info.getLimit());
		List<SysRoleInfo> lists = sysRoleMapper.selectAllData(info);
		return new LayTableResponse<SysRoleInfo>(lists);
	}

	@Override
	@Transactional
	public Response roleInt(Integer roleId, String id) {
		int roleCount = sysRoleMenuMapper.selectDataCountByRoleId(roleId);
		if(id == null || id =="") {//id 为空 清空权限
			if(roleCount == 0) {
				return new Response<String>(ResultEnum.SUCCESS);
			}
			int count = sysRoleMenuMapper.delByRoleId(roleId);
			if(count != roleCount) {
				logger.error("权限设置错误消息:删除条目:delcount={},拥有数目:rolecount={},roleId={}",count,roleCount,roleId);
				throw new RuntimeException("菜单设置时删除数目错误,delcount="+count+"roleId="+roleId);
			}
			return new Response<String>(ResultEnum.SUCCESS);
		}else {//不为空 设置权限
			String[] ids = id.split(",");
			int length = ids.length;
			if(roleCount != 0) {
				int count = sysRoleMenuMapper.delByRoleId(roleId);//清空原有的权限
				if(count != roleCount) {
					logger.error("权限设置错误消息:删除条目:delcount={},拥有数目:rolecount={},roleId={}",count,roleCount,roleId);
					throw new RuntimeException("菜单设置时删除数目错误,delcount="+count+"roleId="+roleId);
				}
			}
				List<SysRoleMenu> roleList = new ArrayList<SysRoleMenu>();
				for(int i=0;i<length;i++) {
				 SysRoleMenu roleMenu = new SysRoleMenu();
				 roleMenu.setRole_id(roleId);
				 roleMenu.setMenu_id(Integer.parseInt(ids[i]));
				 roleMenu.setCtime(new Date());
				 roleList.add(roleMenu);
				}
				int incount = sysRoleMenuMapper.insertList(roleList);
				if(incount != length) {
					 logger.error("编辑菜单权限错误，插入数据不对updatecount={},提交新增数据条数：{}",incount,length);
			           throw new RuntimeException("编辑菜单权限错误错误，更新数据不对insercount="+incount);
				}
				return new Response<String>(ResultEnum.SUCCESS);
		}
	}

	@Override
	public List<SysRole> getRoleByCompanyId(Integer companyId) {
		// TODO Auto-generated method stub
		return null;
	}

    
}
